<template>
  <div>
    <!-- 头部搜索区域 -->
    <el-form inline :model="searchMap" ref="searchMap" class="demo-form-inline" style="margin-top:10px">
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item prop="linkman" v-if="!isDialog">
        <el-input v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" v-if="!isDialog">
        <el-input v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="getData"></el-button>
        <el-button v-if="!isDialog" icon="el-icon-circle-plus-outline" type="primary" @click="addSupplier"></el-button>
        <el-button v-if="!isDialog" icon="el-icon-refresh" type="primary" @click="resetForm('searchMap')"></el-button>
      </el-form-item>
    </el-form>
    <!-- 表单区域 -->
    <el-table :data="list" border style="width: 100%" :highlight-current-row="isDialog"
      @current-change="dataCurrentChange">
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="linkman" label="姓名"></el-table-column>
      <el-table-column prop="mobile" v-if="!isDialog" label="联系电话"></el-table-column>
      <el-table-column prop="name" label="供应商名称"></el-table-column>
      <el-table-column prop="remark" v-if="!isDialog" label="供应物品"></el-table-column>
      <el-table-column label="操作" v-if="!isDialog" width="120">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editSupplier(scope.row.id)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteSupplier(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <!-- 作为子组件的分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
      :layout="!isDialog?'total, sizes, prev, pager, next, jumper':' prev, pager, next'" :total="total"></el-pagination>


    <!-- 弹出层 -->
    <el-dialog v-if="!isDialog" :title="addList.id?'编辑供应商':'新增供应商'" :visible.sync="addDialogVisible" @close="resetAdd"
      width="550px">
      <el-form :model="addList" ref="addList" label-width="100px" label-position="right" style="width:400px">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="addList.name"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="addList.linkman"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="addList.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="addList.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addList.id?submitEdit(addList):submitAdd(addList)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import supplierApi from '@/api/supplier'
  export default {
    props: {
      isDialog: Boolean // 接收父组件传递的数据
    },
    data() {
      return {
        // 供应商搜索
        searchMap: {
          name: '',
          inkman: '',
          mobile: '',
          linkman: '',
        },
        // 表单内容
        list: [],
        // 总条数
        total: 0,
        // 当前页码
        currentPage: 1,
        // 每页显示10条数据,
        pageSize: 10,
        // 弹出窗
        addDialogVisible: false,
        // 编辑/新增供应商
        addList: {
          id: null,
          name: '',
          mobile: '',
          remark: ''

        }
      }
    },
    created() {
      this.getData()
    },

    methods: {
      // 获取数据
      getData() {
        supplierApi.search(this.currentPage, this.pageSize, this.searchMap).then(res => {
          console.log(res.data.data);
          this.total = res.data.data.total
          this.list = res.data.data.rows
        })
      },
      // 新增
      addSupplier() {
        this.addDialogVisible = true
      },
      // 表单重置
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.addList={}
      },
      resetAdd() {
        this.$refs.addList.resetFields();
        this.addList = {}
      },
      // 编辑用户
      editSupplier(id) {
        supplierApi.getById(id).then(res => {
          this.addList = res.data.data
          this.addDialogVisible = true
        })
      },
      // 提交编辑
      submitEdit(data) {
        supplierApi.update(data).then(res => {
          console.log('编辑', res.data);
          //   this.addDialogVisible = false
        })

      },
      // 提交新增
      submitAdd(data) {
        this.addDialogVisible = false
        supplierApi.add(data).then(res => {
          console.log('新增', res.data);
          this.getData()
        })

      },
      // 删除供应商
      deleteSupplier(id) {
        console.log(id);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          supplierApi.delete(id).then(res => {
            console.log(res.data);
          })
          this.getData()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      // 分页功能
      handleSizeChange(val) {
        console.log("每页显示条数", val);
        this.pageSize = val;
        this.getData();
      },
      handleCurrentChange(val) {
        console.log("当前页码", val);
        this.currentPage = val;
        this.getData();
      },

      //  点击某一行触发的事件
      dataCurrentChange(row, oldrow) {
        console.log(row); // 获取到了当前行
        // 把这行数据传递给父亲组件--goods.vue
        this.$emit('option-supplier', row)
      }

    }
  }
</script>

<style lang='less' scoped>

</style>